package com.net.xpay.core.manager.impl;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.net.common.annotation.ReadTransactional;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.TaokeDetectionRecordMapper;
import com.net.xpay.core.domain.TaokeDetectionRecord;
import com.net.xpay.core.manager.TaokeDetectionRecordManager;
import com.net.xpay.core.model.TaokeDetectionRecordSearchModel;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.time.LocalDateTime;
import java.util.List;


@Component
@ReadTransactional
public class TaokeDetectionRecordManagerImpl extends AbstractManager<TaokeDetectionRecord> implements TaokeDetectionRecordManager{

    @Autowired
    private TaokeDetectionRecordMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(TaokeDetectionRecord obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getSerialNumberByTableName(getTableFullName()));
        return super.save(obj);
    }

    @Override
    protected BasicMapper<Long, TaokeDetectionRecord> getMapper() {
        return mapper;
    }

    @Override
    public TaokeDetectionRecord findByTid(Long userId, String tid) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(tid));
        TaokeDetectionRecord condition = new TaokeDetectionRecord();
        condition.setUserId(userId);
        condition.setTid(tid);
        return getByCondition(condition);
    }

    @Override
    public int batchSave(List<TaokeDetectionRecord> records) {
        if (CollectionUtils.isEmpty(records)) {
            return 0;
        }
        for (TaokeDetectionRecord record : records) {
            record.setId(serialNumberService.getSerialNumberByTableName(getTableFullName()));
            LocalDateTime now = LocalDateTime.now();
            record.setModifyTime(now);
            record.setCreateTime(now);
        }
        return mapper.batchSave(records);
    }

    @Override
    public List<TaokeDetectionRecord> listByBatchId(Long userId, Long batchId) {
        Preconditions.checkArgument(userId != null && batchId != null);
        TaokeDetectionRecord condition = new TaokeDetectionRecord();
        condition.setUserId(userId);
        condition.setBatchId(batchId);
        return listByCondition(condition, Lists.newArrayList());
    }

    @Override
    public List<TaokeDetectionRecord> search(TaokeDetectionRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "asc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(TaokeDetectionRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }
}
